﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblCPerson_CommMemberDAL:ItblCPerson_CommMember
    {
        HKEA_CMSEntities model;
        tblCPerson_CommMember source;

        public tblCPerson_CommMemberDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static tblCPerson_CommMemberDAL GetInstance()
        {
            return new tblCPerson_CommMemberDAL();
        } 

        public int InsertENT(tblCPerson_CommMember ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblCPerson_CommMember.AddObject(ent);
            return model.SaveChanges();
        }

        public List<tblCPerson_CommMember> GetListByENT(tblCPerson_CommMember ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.tblCPerson_CommMember.Where(a => a.Cperson_CommMemberUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.Cperson_CommMemberUID > 0)
                result = result.Where(a => a.Cperson_CommMemberUID == ent.Cperson_CommMemberUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public List<tblCPerson_CommMember> GetListByCust_CPerson(tblCust_ContactPerson ent,bool isToCP)
        {
            var ret=from cp_comm in model.tblCPerson_CommMember
                    join cust_cp in model.tblCust_ContactPerson
                    on cp_comm.ContactPersonUID equals cust_cp.ContactPersonUID
                    where cp_comm.Deleted==0 && cust_cp.Deleted==0 && cust_cp.CustomerUID == ent.CustomerUID
                    select cp_comm;
            if (isToCP)
                ret = ret.Where(a=>a.ContactPersonUID==ent.ContactPersonUID);
            return ret.ToList();
        }

        public List<tblCPerson_CommMember> GetListByENT(tblCPerson_CommMember ent)
        {
            var result = model.tblCPerson_CommMember.Where(a => a.Cperson_CommMemberUID > 0).Where(a=>a.Deleted==ent.Deleted);
            if (ent.Cperson_CommMemberUID > 0)
                result = result.Where(a => a.Cperson_CommMemberUID == ent.Cperson_CommMemberUID);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a=>a.ContactPersonUID==ent.ContactPersonUID);
            return result.ToList();
        }



        public int GetCountByENT(tblCPerson_CommMember ent)
        {
            var result = model.tblCPerson_CommMember.Where(a => a.Cperson_CommMemberUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.Cperson_CommMemberUID > 0)
                result = result.Where(a => a.Cperson_CommMemberUID == ent.Cperson_CommMemberUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(tblCPerson_CommMember ent)
        {
            source = model.tblCPerson_CommMember.Where(a=>a.Cperson_CommMemberUID==ent.Cperson_CommMemberUID).FirstOrDefault();
            if (source != null)
            {
                source.Deleted = ent.Deleted;
                source.LastModifyBy = ent.LastModifyBy;
                source.LastModifyDate = DateTime.Now;
                source.CommPositionUID = ent.CommPositionUID;
                source.CommTypeUID = ent.CommTypeUID;
                //source.ContactPersonUID = ent.ContactPersonUID;
                source.Remarks = ent.Remarks;
                source.StartDate = ent.StartDate;
                source.EndDate = ent.EndDate;
                source.Year = ent.Year;
            }
            return model.SaveChanges();
        }

        public List<CommitteeMemberList> GetListByCPUID(int CPUID, List<int> withOutList)
        {
            var list = from comm in model.tblCPerson_CommMember
                       where comm.ContactPersonUID == CPUID && comm.Deleted == 0
                       select new CommitteeMemberList()
                       {
                           KeyUID=comm.Cperson_CommMemberUID,
                           AppCPUID = 0,
                           AppKeyUID = 0,
                           mtblType = comm.mtblCommType,
                           mtblPosition = comm.mtblCommPosition,
                           EndDate = comm.EndDate,
                           From = "tbl",
                           IsDeleted = comm.Deleted,
                           Remarks = comm.Remarks,
                           StartDate = comm.StartDate,
                           TblCPUID = comm.ContactPersonUID,
                           TblKeyUID = comm.Cperson_CommMemberUID,
                           Year = comm.Year,
                       };
            if (withOutList.Count > 0)
                list = list.Where(a=>!withOutList.Contains(a.TblKeyUID));
            return list.ToList();
        }

        public int DeleteENT(tblCPerson_CommMember ent)
        {
            var temp = model.tblCPerson_CommMember.Where(a => a.Cperson_CommMemberUID > 0);
            if (ent.Cperson_CommMemberUID > 0)
                temp = temp.Where(a=>a.Cperson_CommMemberUID==ent.Cperson_CommMemberUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblCPerson_CommMember.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
